package net.wisos.opn.web.account;

import net.wisos.opn.account.service.AccountManager;
import net.wisos.opn.account.entity.Permission;
import net.wisos.opn.account.entity.Role;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping(value = "/account/role/")
public class RoleDetailController {

	@Autowired
	private AccountManager accountManager;

	@RequiresPermissions("role:edit")
	@RequestMapping(value = "update/{id}")
	public String updateForm(Model model) {
		model.addAttribute("allPermissions", Permission.values());
		return "account/roleForm";
	}

	@RequiresPermissions("role:edit")
	@RequestMapping(value = "save/{id}")
	public String save(@ModelAttribute("role") Role role,
			RedirectAttributes redirectAttributes) {
		accountManager.saveRole(role);
		redirectAttributes.addFlashAttribute("message",
				"修改权限组" + role.getName() + "成功");
		return "redirect:/account/role/";
	}

	@ModelAttribute("role")
	public Role getRole(@PathVariable("id") Long id) {
		return accountManager.getRole(id);
	}
}
